package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedis;
    @Autowired
    private CheckGroupService checkGroupService;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        setGroupAndSetmealById(setmeal.getId(),checkgroupIds);
        jedis.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        jedis.close();
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.findAll(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        List<CheckGroup> checkGroups = setmeal.getCheckGroups();
        for (CheckGroup checkGroup : checkGroups) {
            CheckGroup group = checkGroupService.findById(checkGroup.getId());
            checkGroup.setCheckItems(group.getCheckItems());
        }
        return setmeal;
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //获取数据库中的img路径
        Setmeal dataSetmeal = findById(setmeal.getId());

        //编辑检查套餐中的数据
        setmealDao.edit(setmeal);

        if(!(dataSetmeal.getImg() != null && dataSetmeal.getImg().equals(setmeal.getImg()))) {
            QiniuUtils.deleteFileFromQiniu(dataSetmeal.getImg());
            jedis.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,dataSetmeal.getImg());
            jedis.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,dataSetmeal.getImg());
        }
        //删除数据库中的关联关系
        setmealDao.deleteBygroupid(setmeal.getId());

        setGroupAndSetmealById(setmeal.getId(),checkgroupIds);
        jedis.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        jedis.close();
    }

    @Override
    public void delete(Integer id) {
        //删除关联关系表中的数据
        setmealDao.deleteBygroupid(id);
        //删除数据库表的数据
        setmealDao.delete(id);
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
        return setmealDao.findAllSetmeal();
    }

    @Override
    public Setmeal findBySeatmealId(Integer id) {
        return setmealDao.findBySeatmealId(id);
    }

    private void setGroupAndSetmealById(Integer id, Integer[] checkgroupIds) {
        if(checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.setGroupAndSetmealById(id,checkgroupId);
            }
        }
    }
}
